<?php
namespace Syrius\Validator;
/**
 * Date: 19/09/11
 * Time: 7.47
 * @package     Validator
 * @author      Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link        http://code.google.com/p/syrius/
 */

class MaxLengthValidator extends Validator {
    /**
     * Indica se il controllo è valido o meno
     * @param value
     * @return bool
     */
    public function isValid() {
        if (strlen($this -> getValue()) <= $this -> _length) return true;
        throw new \Syrius\Exception\Validation($this -> getMessage());
    }

    /**
     * @param $value valore da controllare
     * @param $length lunghezza massima
     * @param $message personalizzazione del messaggio di errore
     */
    public function __construct($value, $length, $message = "") {
        $this->_length = $length;
        $this->setValue($value);
        $this->setMessage($message);
        $this->isValid();
    }

    /**
     * @return string
     */
    public function getMessage() {
        $str = parent::getMessage();
        $str = str_replace("{len}", $this->_length, $str);
        return $str;
    }

}
